using System;
using System.Collections.Specialized;
using System.Text;

namespace Antix.Mail
{
    public class MessageContentHeaders : NameValueCollection
    {
        #region Constructor

        public MessageContentHeaders(string data)
        {
            // join folded headers
            data = data.Replace("\r\n\t", "").Replace("\r\n ", "");

            // parse into collection
            foreach (string header in data.Split('\n'))
            {
                string[] nameValue = header.Split(':');
                string name = nameValue[0].Trim().ToLower();
                string value = nameValue.Length == 2 ? nameValue[1].Trim() : "";

                if (name.Length > 0) { Add(name, value); }
            }
        }

        #endregion

        #region Properties

        #region ContentType

        /// <summary>
        /// Content type e.g. text/plain, text/html etc.
        /// </summary>
        internal string ContentType
        {
            get
            {
                string value = base[MessageContentHeaderType.ContentType];
                if (value == null)
                {
                    return null;
                }
                else
                {
                    return TextFunctions.Head(value, ";").ToLower();
                }
            }
        }

        #region Boundary

        /// <summary>
        /// Boundry for multipart data
        /// </summary>
        internal string Boundary
        {
            get { return ParseItem(base[MessageContentHeaderType.ContentType], MessageContentHeaderType.Boundary); }
        }

        #endregion

        #endregion

        #region Disposition

        /// <summary>
        /// Content disposition
        /// </summary>
        internal string Disposition
        {
            get
            {
                string value = base[MessageContentHeaderType.ContentDisposition];
                if (value == null)
                {
                    return null;
                }
                else
                {
                    return TextFunctions.Head(value, ";").ToLower();
                }
            }
        }

        #region FileName

        /// <summary>
        /// Present for attachments
        /// </summary>
        internal string FileName
        {
            get { return ParseItem(base[MessageContentHeaderType.ContentDisposition], MessageContentHeaderType.FileName); }
        }

        #endregion

        #endregion

        #region Encoding

        /// <summary>
        /// Used for html encoding
        /// </summary>
        internal string Encoding
        {
            get { return base[MessageContentHeaderType.Encoding]; }
        }

        #endregion

        #endregion

        #region Methods

        /// <summary>
        /// Parse the header, return the requested item
        /// </summary>
        /// <param name="header">Header</param>
        /// <param name="item">Item name</param>
        /// <returns>Item value</returns>
        internal string ParseItem(string header, string item)
        {
            if (header != null && header.IndexOf(item + "=") > -1)
            {
                return TextFunctions.Head(TextFunctions.Tail(header, item + "="), ";").Replace("\"", "");
            }

            return null;
        }

        #endregion
    }
}
